﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;

namespace BHair.Base
{
    public partial class frmjiaoban : Form
    {
        public frmjiaoban()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        string banzu = "";
        private void frmjiaoban_Load(object sender, EventArgs e)
        {
            lbsyy.Text = chuanzhi.userID;
            selectall();

        }
        /// <summary>
        /// 查询并显示
        /// </summary>
        public void selectall()
        {
            try
            {
                //查询开班时间借零金额班组
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = string.Format("SELECT [班别] ,[借零] ,[开班时间] FROM [BHairDB].[dbo].[LoginUser] where LoginUserID='{0}'", chuanzhi.userID);
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                while (sdr.Read())
                {
                    lbkaibantime.Text = sdr["开班时间"].ToString();
                    lbjieling.Text = sdr["借零"].ToString();
                    banzu = sdr["班别"].ToString();
                }
                //根据班组查询
                SqlConnection conn1 = new SqlConnection(constr);
                conn1.Open();
                string sql1 = string.Format("SELECT COUNT(*)FROM [BHairDB].[dbo].[deposit] where 班组='{0}'", chuanzhi.banzu);
                SqlCommand comm1 = new SqlCommand(sql1, conn1);
                lbcz.Text = comm1.ExecuteScalar().ToString();
                string sql2 = string.Format("SELECT COUNT(*) FROM [BHairDB].[dbo].[计次充值] where [班组]='{0}'", chuanzhi.banzu);
                SqlConnection conn2 = new SqlConnection(constr);
                conn2.Open();
                SqlCommand comm2 = new SqlCommand(sql2, conn2);
                lbcc.Text = comm2.ExecuteScalar().ToString();
                string sql3 = string.Format("SELECT COUNT(*) FROM [BHairDB].[dbo].[销售表] where [班组]='{0}'", chuanzhi.banzu);
                SqlConnection conn3 = new SqlConnection(constr);
                conn3.Open();
                SqlCommand comm3 = new SqlCommand(sql3, conn3);
                lbxf.Text = comm3.ExecuteScalar().ToString();
                string sql4 = string.Format("SELECT COUNT(*) FROM [BHairDB].[dbo].[jcxf] where [班组]='{0}'", chuanzhi.banzu);
                SqlConnection conn4 = new SqlConnection(constr);
                conn4.Open();
                SqlCommand comm4 = new SqlCommand(sql4, conn4);
                lbjc.Text = comm4.ExecuteScalar().ToString();
                string sql5 = string.Format("SELECT COUNT(*) FROM [BHairDB].[dbo].[pays] where [班组]='{0}'", chuanzhi.banzu);
                SqlConnection conn5 = new SqlConnection(constr);
                conn5.Open();
                SqlCommand comm5 = new SqlCommand(sql5, conn5);
                lbkk.Text = comm5.ExecuteScalar().ToString();
                string sql6 = string.Format("SELECT SUM([skje]) FROM [BHairDB].[dbo].[收入明细] where 班组='{0}' and skfs='现金'", chuanzhi.banzu);
                SqlConnection conn6 = new SqlConnection(constr);
                conn6.Open();
                SqlCommand comm6 = new SqlCommand(sql6, conn6);
                string ifa =comm6.ExecuteScalar().ToString();
                
                if (ifa != string.Empty)
                {
                    lbxj.Text = comm6.ExecuteScalar().ToString();
                }
                else
                {
                    lbxj.Text = "0.00";
                }
                
                string sql7 = string.Format("SELECT SUM([skje]) FROM [BHairDB].[dbo].[收入明细] where 班组='{0}' and skfs='微信'", chuanzhi.banzu);
                SqlConnection conn7 = new SqlConnection(constr);
                conn7.Open();
                SqlCommand comm7 = new SqlCommand(sql7, conn7);
                string ifb =comm7.ExecuteScalar().ToString();
                if ( ifb!= string.Empty)
                {
                    lbwx.Text = comm7.ExecuteScalar().ToString();
                }
                else
                {
                    lbwx.Text = "0.00";
                }
                conn.Close();
                conn1.Close();
                conn2.Close();
                conn3.Close();
                conn4.Close();
                conn5.Close();
                conn6.Close();
                conn7.Close();
            }
            catch (Exception ee) { MessageBox.Show(ee.Message); }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (chuanzhi.banzu != string.Empty)
            {
                try
                {
                    //插入交班表
                    string sql2 = string.Format("INSERT INTO [BHairDB].[dbo].[交班表] ([班组号] ,[职员]  ,[开班时间] ,[交班时间],[会员充值笔数] ,[会员充次笔数] ,[会员消费笔数]  ,[直接扣款笔数] ,[计次项目消费笔数] ,[现金收款] ,[微信收款] ,[开班借零]) VALUES ('{0}' ,'{1}' ,'{2}' ,'{3}',{4} ,{5} ,{6} ,{7} ,{8} ,'{9}' ,'{10}' ,'{11}')", chuanzhi.banzu, chuanzhi.userID, lbkaibantime.Text, DateTime.Now, lbcz.Text, lbcc.Text, lbxf.Text, lbkk.Text, lbjc.Text, lbxj.Text, lbwx.Text, lbjieling.Text);
                    SqlConnection conn2 = new SqlConnection(constr);
                    conn2.Open();
                    SqlCommand comm2 = new SqlCommand(sql2, conn2);
                    comm2.ExecuteNonQuery();
                    string sql = string.Format("UPDATE [BHairDB].[dbo].[LoginUser] SET  [开班] =0,[班别] ='' ,[借零] =0 ,[开班时间] ='' WHERE LoginUserID='{0}'", chuanzhi.userID);
                    SqlConnection conn = new SqlConnection(constr);
                    conn.Open();
                    SqlCommand comm = new SqlCommand(sql, conn);
                    comm.ExecuteNonQuery();
                    chuanzhi.banzu = string.Empty;

                    //重新生成单据规则
                    string sql1 = "UPDATE [BHairDB].[dbo].[djh]SET [lsdjh] = '00000' ,[jhdjh] = '00000' ,[czdjh] = '00000' ,[jcxf] = '00000' ,[xuqi] = '00000' ,[hycz] = '00000',[jccz]='00000' ";
                    SqlConnection conn1 = new SqlConnection(constr);
                    conn1.Open();
                    SqlCommand comm1 = new SqlCommand(sql1, conn1);
                    comm1.ExecuteNonQuery();


                    MessageBox.Show("交班成功");
                    button1.Visible = true;
                    conn.Close();
                    conn1.Close();
                }
                catch (Exception ee) { MessageBox.Show(ee.Message); }
            }
            else
            {
                MessageBox.Show("当前未开班，无需交班！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dy();
        }
        public void dy()
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\交班.repx");
            xr.DataSource = savedy();
             xr.ShowPreview();
            //xr.Print();
        }
        public DataSet savedy()
        {
            DataSet ds = new DataSet();
            string sql = string.Format("SELECT [班组号] ,[职员] ,[开班时间] ,[交班时间] ,[会员充值笔数],[会员充次笔数] ,[会员消费笔数] ,[直接扣款笔数] ,[计次项目消费笔数] ,[现金收款] ,[微信收款],[开班借零] FROM [BHairDB].[dbo].[交班表] where 班组号='{0}'",banzu);
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                sda.Fill(ds, "交班表");
                conn.Close();
            }
            catch (Exception ee){ MessageBox.Show(ee.Message); }
            return ds;
        }
    }
}
